package com.imooc.framework.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLockDemo implements Runnable{

    private static ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
    private static Lock rlock = reentrantReadWriteLock.readLock();
    private static Lock wlock = reentrantReadWriteLock.writeLock();

    private static Integer count = 0;


    @Override
    public void run() {
        rlock.lock();
        try {
            for (int i = 0; i < 10; i++) {
                count++;
                System.out.println(Thread.currentThread().getName() + " param " + count);
            }
        } finally {
            rlock.unlock();;
        }
    }

    public static void main(String[] args) {
        ReadWriteLockDemo reentrantReadWriteLockDemo = new ReadWriteLockDemo();
        ReadWriteLockDemo reentrantReadWriteLockDemo1 = new ReadWriteLockDemo();
        Thread t1 = new Thread(reentrantReadWriteLockDemo);
        Thread t2 = new Thread(reentrantReadWriteLockDemo);
        Thread t3 = new Thread(reentrantReadWriteLockDemo);
        t1.start();
        t2.start();
        t3.start();
    }
}
